/**
 * 药品库
 */
Lib = cc.Sprite.extend({
	libArr: [],
	openFlag:false,
	doing:false,
	rm : 5,
	ctor:function(p){
		this._super("#lib_back.png");
		p.addChild(this,30);
		this.setTag(TAG_LIB);
		this.init();
	},
	init:function(){
		this.libArr = [];
		this.openFlag = false;
		this.setPosition(gg.width + this.width * 0.5,550);
		
		var instrument=new LibButton(this,10,TAG_LIB_INSTRUMENT,"#Instrument.png",this.callback,this);
		instrument.setPosition(cc.p(60,this.height*0.5));

		var electrode=new LibButton(this,10,TAG_LIB_ELECTRODE,"#electrode.png",this.callback,this);
		electrode.right(instrument,this.rm);
		
		var bottle=new LibButton(this,10,TAG_LIB_BOTTLE,"#bottle.png",this.callback,this);
		bottle.right(electrode,this.rm);
		
		var battery=new LibButton(this,10,TAG_LIB_BATTERY,"#Battery.png",this.callback,this);
		battery.right(bottle,this.rm);
		
		var film=new LibButton(this,10,TAG_LIB_FILM,"#film.png",this.callback,this);
		film.right(battery,this.rm);
		
		var kettle=new LibButton(this,10,TAG_LIB_KETTLE,"#kettle.png",this.callback,this);
		kettle.right(film,this.rm);
		
		var beaker=new LibButton(this,10,TAG_LIB_BEAKER,"#beaker.png",this.callback,this);
		beaker.right(kettle,this.rm);
		
		var bottle1=new LibButton(this,10,TAG_LIB_BOTTLE1,"#bottle1.png",this.callback,this);
		bottle1.right(beaker,this.rm);
	},
	moveLib:function(tag,width){
		width = 75;
		var begin = false;
		for(var i in this.libArr){
			var libTag = this.libArr[i];
			if(tag == libTag){
				begin = true;
			}
			if(begin){
				var lib = this.getChildByTag(libTag);
				if(lib != null){
					lib.runAction(cc.moveBy(0.5,cc.p(-width, 0)));
				}
			}
		}
	},
	callback:function(p){
		var pos = this.getPosition(); 
		var action = gg.flow.flow.action;
		switch(p.getTag()){
		case TAG_LIB_INSTRUMENT:
			ll.run.loadInstrument(pos);
			break;
		case TAG_LIB_ELECTRODE:
			ll.run.loadElectrode(pos);
			break;
		case TAG_LIB_BOTTLE:
			ll.run.loadBottle(pos);
			break;
		case TAG_LIB_BATTERY:
			ll.run.loadBattery(pos);
			break;
		case TAG_LIB_FILM:
			ll.run.loadFilm(pos);
			break;
		case TAG_LIB_KETTLE:
			ll.run.loadKettle(pos);
			break;
		case TAG_LIB_BEAKER:
			ll.run.loadBeaker(pos);
			break;
		case TAG_LIB_BOTTLE1:
			ll.run.loadBottle1(pos);
			break;
		default:
			break;
		}
		if(action == ACTION_NONE){
			this.moveLib(p.getTag(), p.width * p.getScale());
			p.removeFromParent(true);
		}
	},
	isOpen:function(){
		return this.openFlag; 
	},
	open:function(){
		if(this.openFlag || this.doing){
			return;
		}
		this.doing = true;
		var move = cc.moveBy(0.4, cc.p(-this.width,0));
		var func = cc.callFunc(function(){
			this.openFlag = true;
			this.doing = false;
			var tag = gg.flow.flow.tag;
			 if(tag instanceof Array){
				 if(TAG_LIB_MIN < tag[1]){
					 // 显示箭头
					 gg.flow.location();
				 }
			 }
		}, this);
		var seq = cc.sequence(move,func);
		this.runAction(seq);
	},
	close:function(){
		if(!this.openFlag || this.doing){
			return;
		}
		this.doing = true;
		var move = cc.moveBy(0.4, cc.p(this.width,0));
		var func = cc.callFunc(function(){
			this.openFlag = false;
			this.doing = false;
			var tag = gg.flow.flow.tag;
			if(tag instanceof Array){
				if(TAG_LIB_MIN < tag[1]){
					// 隐藏箭头
//					ll.tip.arr.out();
					ll.tip.arr.pos(ll.tool.getChildByTag(TAG_BUTTON_LIB));	
				}
			}
		}, this);
		var seq = cc.sequence(move,func);
		this.runAction(seq);
	}
});
TAG_LIB_MIN = 30000;
TAG_LIB_INSTRUMENT=30001;
TAG_LIB_ELECTRODE=30002;
TAG_LIB_BOTTLE =30003;
TAG_LIB_BATTERY=30004;
TAG_LIB_FILM=30005;
TAG_LIB_KETTLE=30006;
TAG_LIB_BEAKER=30007;
TAG_LIB_BOTTLE1=30008;
libRelArr = [
             {tag:TAG_LIB_MIN, name:""},
             {tag:TAG_LIB_INSTRUMENT, name:"溶解氧仪"},
             {tag:TAG_LIB_ELECTRODE, name:"电极"},
             {tag:TAG_LIB_BOTTLE, name:"校准液瓶"},
             {tag:TAG_LIB_BATTERY, name:"电池"},
             {tag:TAG_LIB_FILM, name:"薄膜"},
             {tag:TAG_LIB_KETTLE,name:"蒸馏水瓶"},
             {tag:TAG_LIB_BEAKER,name:"烧杯"},
             {tag:TAG_LIB_BOTTLE1,name:"水样瓶"}];
